package org.wwb.antbox.user.service.role;

import org.ant.commons.commons.query.enums.Sort;
import org.ant.commons.models.model.BaseIdModel;
import org.ant.commons.models.page.PageInfo;
import org.ant.commons.models.result.BaseEditResult;
import org.ant.commons.models.result.BaseListResult;
import org.ant.commons.models.result.BasePageResult;
import org.ant.commons.models.result.BaseResult;
import org.ant.mybatis.query.Query;
import org.ant.service.common.BaseService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.wwb.antbox.user.data.role.SysRoleMapper;
import org.wwb.antbox.user.iservice.role.ISysRoleService;
import org.wwb.antbox.user.iservice.rolemenu.ISysRoleMenuService;
import org.wwb.antbox.user.model.role.entity.SysRole;
import org.wwb.antbox.user.model.role.enums.SysRoleStatusEnum;
import org.wwb.antbox.user.model.role.model.SysRoleAddModel;
import org.wwb.antbox.user.model.role.model.SysRoleDelModel;
import org.wwb.antbox.user.model.role.model.SysRoleEditModel;
import org.wwb.antbox.user.model.role.model.SysRolePageModel;
import org.wwb.antbox.user.model.role.view.RoleListView;
import org.wwb.antbox.user.model.role.view.SysRolePageView;
import org.wwb.antbox.user.model.rolemenu.model.SysRoleMenuAddModel;
import org.wwb.antbox.user.model.rolemenu.model.SysRoleMenuDelModel;
import java.util.List;

/**
 * @Author Bond
 * @Date: 2019/10/4 20:00
 * @Description: 系统角色接口实现
 */
@Service(registry = {"registryConfigUser"},provider ="dubboProviderUser")
public class SysRoleServiceImpl extends BaseService<SysRoleMapper, SysRole, String> implements ISysRoleService {

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    /**
     * 系统角色分页查询
     * @param sysRolePageModel 分页查询参数模型
     * @return BasePageResult<SysRolePageView>
     */
    @Override
    public BasePageResult<SysRolePageView> getSysRolePage(SysRolePageModel sysRolePageModel) {
        Query build = Query.build(sysRolePageModel)
                .orderby("role_sort", Sort.ASC)
                .page(sysRolePageModel.getPageIndex(),sysRolePageModel.getPageSize());

        PageInfo<SysRole> sysRolePageInfo = queryPage(build);
        PageInfo<SysRolePageView> sysRolePageViewPageInfo = new SysRolePageView().toPageView(sysRolePageInfo);

        BasePageResult<SysRolePageView> basePageResult = new BasePageResult<>();
        basePageResult.setModel(sysRolePageViewPageInfo);
        basePageResult.setSuccess(true);
        return basePageResult;
    }

    @Override
    public BaseResult<String> saveSysRole(SysRoleAddModel sysRoleAddModel) {
        /**
         * 1、先添加角色
         */
        SysRole sysRole = sysRoleAddModel.modelToEntity();
        sysRole.setRoleStatus(SysRoleStatusEnum.NORMAL.getCode());
        int save = mapper.save(sysRole);

        /**
         * 2、再添加角色下的权限  不为空，则证明有要添加的权限功能
         */
        if(!sysRoleAddModel.getMenuIds().isEmpty()){
            SysRoleMenuAddModel sysRoleMenuAddModel = new SysRoleMenuAddModel();
            sysRoleMenuAddModel.setRoleId(sysRole.getId());
            sysRoleMenuAddModel.setMenuIds(sysRoleAddModel.getMenuIds());
            sysRoleMenuService.saveSysRoleMenu(sysRoleMenuAddModel);
        }


        BaseResult<String> baseResult = new BaseResult<>();
        baseResult.setModel(sysRole.getId());
        baseResult.setSuccess(save == 1);
        return baseResult;
    }

    @Override
    public BaseResult<Integer> delSysRole(SysRoleDelModel sysRoleDelModel) {
        /**
         * 1、先删除系统角色菜单关系数据
         */
        SysRoleMenuDelModel sysRoleMenuDelModel = new SysRoleMenuDelModel();
        sysRoleMenuDelModel.setRoleIds(sysRoleDelModel.getIds());
        sysRoleMenuService.delSysRoleMenu(sysRoleMenuDelModel);

        /**
         * 2、再删除角色信息
         */
        Query query = new Query().in("id",sysRoleDelModel.getIds());
        int delete = mapper.deleteByQuery(query);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(delete);
        baseResult.setSuccess(delete >= 1);
        return baseResult;
    }

    @Override
    public BaseResult<Integer> disableSysRole(SysRoleDelModel sysRoleDelModel) {
        Query query = new Query().in("id",sysRoleDelModel.getIds());

        SysRole sysRole = new SysRole();
        sysRole.setRoleStatus(SysRoleStatusEnum.DISABLED.getCode());

        int disable = mapper.updateByQuery(sysRole,query);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(disable);
        baseResult.setSuccess(disable >= 1);
        return baseResult;
    }

    @Override
    public BaseResult<Integer> enableSysRole(SysRoleDelModel sysRoleDelModel) {
        Query query = new Query().in("id",sysRoleDelModel.getIds());

        SysRole sysRole = new SysRole();
        sysRole.setRoleStatus(SysRoleStatusEnum.NORMAL.getCode());

        int enable = mapper.updateByQuery(sysRole,query);

        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(enable);
        baseResult.setSuccess(enable >= 1);
        return baseResult;
    }

    @Override
    public BaseEditResult doEditSysRole(BaseIdModel<String> baseIdModel) {
        SysRole sysRole = mapper.getById(baseIdModel.getId());
        BaseEditResult baseEditResult = new BaseEditResult();
        baseEditResult.setModel(sysRole);
        baseEditResult.setSuccess(true);
        return baseEditResult;
    }

    @Override
    public BaseResult<Integer> editSysRole(SysRoleEditModel sysRoleEditModel) {
        // 1、先删除指定的权限
        sysRoleMenuService.delSysRoleMenuByRoleIdAndMenuIds(sysRoleEditModel.getId(),sysRoleEditModel.getMenuIds());

        // 2、再添加角色下的菜单
        if(!sysRoleEditModel.getMenuIds().isEmpty()){
            SysRoleMenuAddModel sysRoleMenuAddModel = new SysRoleMenuAddModel();
            sysRoleMenuAddModel.setRoleId(sysRoleEditModel.getId());
            sysRoleMenuAddModel.setMenuIds(sysRoleEditModel.getMenuIds());
            sysRoleMenuService.saveSysRoleMenu(sysRoleMenuAddModel);
        }

        // 3、再更新角色信息
        SysRole sysRole = sysRoleEditModel.modelToEntity();
        int edit = mapper.updateIgnoreNull(sysRole);
        BaseResult<Integer> baseResult = new BaseResult<>();
        baseResult.setModel(edit);
        baseResult.setSuccess(edit >= 1);
        return baseResult;
    }

    @Override
    public BaseListResult<RoleListView> getSysRoleList4Transfer() {
        Query query = new Query().orderby("role_sort", Sort.ASC);
        // 查询出所有菜单
        List<SysRole> sysRoleList = mapper.list(query);

        BaseListResult<RoleListView> baseResult = new BaseListResult<>();
        List<RoleListView> roleListView = new RoleListView().myListView(sysRoleList);
        baseResult.setModel(roleListView);
        baseResult.setSuccess(true);
        return baseResult;
    }
}
